﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;

namespace BLGPS
{
    
    public class Serializer
    {

        public static T Open<T>(string pPath)
        {
            T instance = default(T);
            try
            {
                XmlSerializer mySerializer = new XmlSerializer(typeof(T));

                using (FileStream myFileStream = new FileStream(pPath, FileMode.Open))
                {
                    instance = (T)mySerializer.Deserialize(myFileStream);
                    myFileStream.Close();
                }

            }
            catch (Exception ex)
            {
                //throw new Exception("No se pudo abrir el archivo",ex);  
            }

            return instance;

        }

        public static void Save(object instance, string pPath)
        {
            XmlSerializer mySerializer = new XmlSerializer(instance.GetType());
            // To write to a file, create a StreamWriter object.
            StreamWriter myWriter = new StreamWriter(pPath);
            mySerializer.Serialize(myWriter, instance);
            myWriter.Close();
        }

    }

    public class Encrypter
    {
        /// <summary>
        /// Encripta un string usando SHA1 ( no se puede volver a desencriptar).
        /// Se utiliza para guardar password,para saber si el pass ingresado es correcto
        /// se lo encripta y se lo compara con el que esta guardado
        /// </summary>
        /// <param name="strToEncrypt"></param>
        /// <returns></returns>
        public static string encryptPass(string strToEncrypt)
        {

            System.Security.Cryptography.HashAlgorithm hashValue = new
            System.Security.Cryptography.SHA1CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strToEncrypt);

            byte[] byteHash = hashValue.ComputeHash(bytes);

            hashValue.Clear();

            return (Convert.ToBase64String(byteHash));
        }
    }
}
